package com.qixin.activiti.listenter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.IActZTaskService;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;

/**
 * @author wmf
 * @date 2022-02-13 16:40
 *  流程节点监听器测试类
 */
@Slf4j
public class MyExecutionListener implements ExecutionListener, TaskListener {

    // 流程业务扩展表
    private ActBusinessServiceImpl actBusinessService;

    // 待办任务扩展表
    private IActZTaskService actZTaskService;


    @Override
    public void notify(DelegateExecution execution) throws Exception {
        // TODO Auto-generated method stub
        //获取业务表相关信息
        String procInstId = execution.getProcessInstanceId(); // 流程实例Id
        String actBusinessId = execution.getProcessBusinessKey(); // 获取业务扩展表Id
//       String activityId = execution.getCurrentActivityId();
//        String processDefinitionId=execution.getProcessDefinitionId(); // 获取流程定义id
//
//        ProcessDefinitionEntity processDefinitionEntity=(ProcessDefinitionEntity) execution.getEngineServices().getRepositoryService()
//                .getProcessDefinition(processDefinitionId);

//        ActivityImpl activityImpl=processDefinitionEntity.findActivity(activityId); // 根据活动id获取活动实例
//        TaskDefinition taskDef = (TaskDefinition)activityImpl.getProperties().get("taskDefinition");
//        String zpr = taskDef.getAssigneeExpression()==null?"":taskDef.getAssigneeExpression().getExpressionText();//代理人
//        Set<Expression> userCodes = taskDef.getCandidateUserIdExpressions();//候选人
//        Set<Expression> roleCodes = taskDef.getCandidateGroupIdExpressions();//候选组
        //获取审批人信息 end

        QueryWrapper<ActBusiness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",actBusinessId);
        actBusinessService = (ActBusinessServiceImpl) SpringContextUtils.getBean("actBusinessServiceImpl");
        ActBusiness actBusiness = actBusinessService.getOne(queryWrapper);
        log.info("actTableName:{}",actBusiness.getTableName());

        // 流程审批结束后执行相应的业务操作，比如把单据推送到ERP

        String eventName = execution.getEventName();
        //start
        if ("start".equals(eventName)) {
            System.out.println("start=========");
            System.out.println("procInstId=========");
            System.out.println("actBusinessId=========");
        }else if ("end".equals(eventName)) {
            System.out.println("end=========");
        }
        else if ("take".equals(eventName)) {
            System.out.println("take=========");
        }
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        // TODO Auto-generated method stub
        String eventName = delegateTask.getEventName();
        if ("create".endsWith(eventName)) {
            System.out.println("create=========");
        }else if ("assignment".endsWith(eventName)) {
            System.out.println("assignment========");
        }else if ("complete".endsWith(eventName)) {
            System.out.println("complete===========");
        }else if ("delete".endsWith(eventName)) {
            System.out.println("delete=============");
        }
    }
}
